package com.andy.common.utils

import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources
import android.os.Build
import android.telephony.TelephonyManager
import toJson
import java.util.Locale


object PhoneUtils {
    /**
     * 获取屏幕高度
     */
    private var screenHeight = 0
    fun screenHeight(): Int {
        if (screenHeight == 0) {
            screenHeight = Resources.getSystem().displayMetrics.heightPixels
        }
        return screenHeight
    }

    /**
     * 获取屏幕宽度
     */
    private var screenWidth = 0
    fun screenWidth(): Int {
        if (screenWidth == 0) {
            screenWidth = Resources.getSystem().displayMetrics.widthPixels
        }
        return screenWidth
    }

    /**
     * 获取当前手机系统语言。
     *
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    fun systemLanguage(): String {
        return Locale.getDefault().language
    }

    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    fun systemVersion(): String {
        return Build.VERSION.RELEASE
    }

    /**
     * 获取手机型号
     *
     * @return  手机型号
     */
    fun systemModel(): String {
        return Build.MODEL
    }

    /**
     * 获取手机设备名
     *
     * @return  手机设备名
     */
    fun deviceName(): String {
        return Build.DEVICE
    }

    /**
     * 获取手机厂商
     *
     * @return  手机厂商
     */
    fun deviceBrand(): String {
        return Build.BRAND
    }

    /**
     * 获取手机主板名
     *
     * @return  主板名
     */
    fun deviceBoardName(): String {
        return Build.BOARD
    }

    /**
     * 获取手机厂商名
     *
     * @return  手机厂商名
     */
    fun deviceManufacturer(): String {
        return Build.MANUFACTURER
    }

    /**
     * 获取用户所在国家
     *
     * @return String?
     */
    fun countryISO(ctx: Context): String? { // 先获取电话供应商
        var country: String? = null
        val mTelephonyManager = ctx.applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?
        if (mTelephonyManager != null) {
            country = mTelephonyManager.networkCountryIso
            if (country.isNullOrEmpty()) country = mTelephonyManager.simCountryIso
            if (country.isNullOrEmpty()) country = Locale.getDefault().country
        }

        return country?.uppercase()
    }


    // 获取应用程序版本名称信息
    fun getVersionName(context: Context): String? {
        try {
            val packageManager = context.packageManager
            val packageInfo = packageManager.getPackageInfo(context.packageName, 0)
            return packageInfo.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return null
    }

    // 获取应用程序版本号
    fun getVersionCode(context: Context): Int {
        try {
            val packageManager = context.packageManager
            val packageInfo = packageManager.getPackageInfo(context.packageName, 0)
            return packageInfo.versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return -1
    }

    // 检查应用程序是否安装
    fun isAppInstalled(context: Context, packageName: String): Boolean {
        return try {
            val packageManager = context.packageManager
            val packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
            packageInfo != null
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            false
        }
    }

    // 获取应用程序名称
    fun getAppName(context: Context): String? {
        val packageManager = context.packageManager
        try {
            val packageInfo = packageManager.getPackageInfo(context.packageName, 0)
            val labelRes = packageInfo.applicationInfo.labelRes
            return context.getString(labelRes)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return null
    }

    // 获取应用程序包名
    fun getPackageName(context: Context): String? {
        return context.packageName
    }

    // 获取SDK版本号
    fun getSDKVersion(): Int {
        return Build.VERSION.SDK_INT
    }

    fun getDeviceInfo(phone: String? = null): String {
        val map = mutableMapOf<String, String>()
        map["brand"] = deviceBrand() //手机厂商
        map["model"] = systemModel() //手机型号
        map["systemLanguage"] = systemLanguage() //当前手机系统语言
        map["systemVersion"] = systemVersion() //系统版本号
        map["deviceName"] = deviceName() //手机设备名
        map["deviceBoardName"] = deviceBoardName() //主板名
        map["deviceManufacturer"] = deviceManufacturer() //手机厂商名
        phone?.let {
            map["phoneNumber"] = phone
        }
        return map.toJson()
    }
}